package com.banzhi.shop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.Login.Login_data;
import com.banzhi.shop.entity.NewLogin.LoginModelImpl;
import com.banzhi.shop.model.CallBack.LoginModelCallBack;
import com.banzhi.shop.utils.ClickUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import static com.banzhi.shop.MyApp.context;

/*
* 密码登录(手机号码登录)
* */
public class MimaLoginActivity extends BaseActivity implements LoginModelCallBack, View.OnClickListener {

    private LoginModelImpl mLoginModel;
    private EditText mEtPhone;
    private ImageView mIvBack;
    private EditText mEtCode;
    private TextView mTvForgetPassword;
    private Button mBtnLogin;
    private TextView mTvYanzhengmaLogin;
    private EditText mEtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_mima_login);
        Translucent.setTranslucent(this);
        mLoginModel = new LoginModelImpl();
        initView();
        setListeners();


    }
    //初始化控件
    private void initView() {
        mIvBack = (ImageView) findViewById( R.id.iv_back );
        mEtPhone = (EditText) findViewById( R.id.et_phone );
        mEtPassword = (EditText) findViewById( R.id.et_password );
        mTvForgetPassword = (TextView) findViewById( R.id.tv_forgetPassword );
        mBtnLogin = (Button) findViewById( R.id.btn_login );
        mTvYanzhengmaLogin = (TextView) findViewById( R.id.tv_yanzhengma_login );
    }
    //设置点击事件
    private void setListeners() {
        mIvBack.setOnClickListener( this );
        mBtnLogin.setOnClickListener( this );
        mTvForgetPassword.setOnClickListener( this );
        mTvYanzhengmaLogin.setOnClickListener( this );
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_login:
                if (ClickUtils.isFastClick()) {
                    String phone = mEtPhone.getText().toString().trim();
                    String password = mEtPassword.getText().toString().trim();
                    if (TextUtils.isEmpty( phone )) {
                        ToastUtils.makeToastShort( "请输入手机号" );
                    } else if (phone.length()<11) {
                        ToastUtils.makeToastShort( "电话号码不符规范" );
                    } else if (TextUtils.isEmpty( password )) {
                        ToastUtils.makeToastShort( "请输入登录密码" );
                    } else {
                        LoadingDialogUtils.showProgress( this,"正在加载,请稍后" );
                        mLoginModel.login( phone,password,this );

                    }
                }
                break;
            case R.id.tv_forgetPassword:
                Intent intent = new Intent( MimaLoginActivity.this, ForgetPasswordActivity.class );
                startActivity( intent );
                break;
            case R.id.tv_yanzhengma_login:
                Intent intent2 = new Intent( MimaLoginActivity.this, YanzhengmaLoginActivity.class );
                startActivity( intent2 );
                break;
        }

    }

    @Override
    public void onloginSuccess(Object object) {
        Login_data login = (Login_data) object;
        ToastUtils.makeToastShort("登录成功");

        SharedPreferences.Editor editor = MyApp.loginMessage.edit();
        editor.putString("token", login.getToken());
        editor.putString("id", login.getId());
        editor.putString("nickname", login.getNickname());
        editor.putString("openid_wx", login.getOpenid_wx());
        editor.putString("openid", login.getOpenid());
        editor.putString("mobile", login.getMobile());
        editor.putString("isagent",login.getIsagent()+"");
        editor.apply();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();

        JPushInterface.setAlias(context,login.getMobile() ,
                new TagAliasCallback() {

                    @Override
                    public void gotResult(int responseCode,
                                          String alias, Set<String> tags) {
                        if (responseCode==0) {
                            System.out.println("jpush alias@@@@@别名设置成功");
                        }
                    }
                });


    }

    @Override
    public void onloginError(Object object) {
        LoadingDialogUtils.dismiss();
        JSONObject object1 = (JSONObject) object;
        ToastUtils.makeToastShort(object1.optString("msg"));

    }



    @Override
    public void onWXloginSuccess(Object object) {

    }

    @Override
    public void onWXloginError(Object object) {

    }

    @Override
    public void onBindingPhoneSuccess(Object object) {

    }

    @Override
    public void onYZMloginSuccess(Object object) {

    }

    @Override
    public void onYZMloginError(Object object) {

    }

    @Override
    public void onBindingLoginSuccess(Object object) {

    }

    @Override
    public void onBindingLoginError(Object object) {

    }
}
